﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace Terraria.ModLoader.IO
{
	// Token: 0x020002CB RID: 715
	public abstract class TagSerializer
	{
		// Token: 0x170001CA RID: 458
		// (get) Token: 0x06001BDB RID: 7131
		public abstract Type Type { get; }

		// Token: 0x170001CB RID: 459
		// (get) Token: 0x06001BDC RID: 7132
		public abstract Type TagType { get; }

		// Token: 0x06001BDD RID: 7133
		public abstract object Serialize(object value);

		// Token: 0x06001BDE RID: 7134
		public abstract object Deserialize(object tag);

		// Token: 0x06001BDF RID: 7135
		public abstract IList SerializeList(IList value);

		// Token: 0x06001BE0 RID: 7136
		public abstract IList DeserializeList(IList value);

		// Token: 0x06001BE1 RID: 7137 RVA: 0x00013FAD File Offset: 0x000121AD
		static TagSerializer()
		{
			TagSerializer.Reload();
		}

		// Token: 0x06001BE2 RID: 7138 RVA: 0x00432A74 File Offset: 0x00430C74
		internal static void Reload()
		{
			TagSerializer.serializers.Clear();
			TagSerializer.typeNameCache.Clear();
			TagSerializer.AddSerializer(new BoolTagSerializer());
			TagSerializer.AddSerializer(new UShortTagSerializer());
			TagSerializer.AddSerializer(new UIntTagSerializer());
			TagSerializer.AddSerializer(new ULongTagSerializer());
			TagSerializer.AddSerializer(new Vector2TagSerializer());
			TagSerializer.AddSerializer(new Vector3TagSerializer());
			TagSerializer.AddSerializer(new ColorSerializer());
			TagSerializer.AddSerializer(new Point16Serializer());
			TagSerializer.AddSerializer(new RectangleSerializer());
		}

		// Token: 0x06001BE3 RID: 7139 RVA: 0x00432AF0 File Offset: 0x00430CF0
		public static bool TryGetSerializer(Type type, out TagSerializer serializer)
		{
			if (TagSerializer.serializers.TryGetValue(type, out serializer))
			{
				return true;
			}
			if (typeof(TagSerializable).IsAssignableFrom(type))
			{
				Type type2 = typeof(TagSerializableSerializer<>).MakeGenericType(new Type[]
				{
					type
				});
				IDictionary<Type, TagSerializer> dictionary = TagSerializer.serializers;
				TagSerializer value;
				serializer = (value = (TagSerializer)type2.GetConstructor(new Type[0]).Invoke(new object[0]));
				dictionary[type] = value;
				return true;
			}
			return false;
		}

		// Token: 0x06001BE4 RID: 7140 RVA: 0x00013FC8 File Offset: 0x000121C8
		public static void AddSerializer(TagSerializer serializer)
		{
			TagSerializer.serializers.Add(serializer.Type, serializer);
		}

		// Token: 0x06001BE5 RID: 7141 RVA: 0x00432B68 File Offset: 0x00430D68
		public static Type GetType(string name)
		{
			Type type;
			if (TagSerializer.typeNameCache.TryGetValue(name, out type))
			{
				return type;
			}
			type = Type.GetType(name);
			if (type != null)
			{
				return TagSerializer.typeNameCache[name] = type;
			}
			foreach (Mod mod in ModLoader.LoadedMods)
			{
				Assembly code = mod.Code;
				type = ((code != null) ? code.GetType(name) : null);
				if (type != null)
				{
					return TagSerializer.typeNameCache[name] = type;
				}
			}
			return null;
		}

		// Token: 0x040018A0 RID: 6304
		private static IDictionary<Type, TagSerializer> serializers = new Dictionary<Type, TagSerializer>();

		// Token: 0x040018A1 RID: 6305
		private static IDictionary<string, Type> typeNameCache = new Dictionary<string, Type>();
	}
}
